package christ.mmd.xml;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import christ.mmd.bl.FileUpdateInfo;

import java.text.ParseException;
import android.text.format.DateFormat;
import android.util.Xml;

public class FilesUpdateInfoXmlParser extends AXmlParser {

	protected List readDataEntities(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		List entries = new ArrayList();

		parser.require(XmlPullParser.START_TAG, ns, "files");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the entry tag
			if (name.equals("file")) {
				entries.add(readFileUpdateEntry(parser));
			} else {
				skip(parser);
			}
		}
		return entries;
	}

	// Processes file tags in the feed.
	private FileUpdateInfo readFileUpdateEntry(XmlPullParser parser)
			throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "file");
		String relType = parser.getAttributeValue(null, "type");
		String link = parser.getAttributeValue(null, "url");
		String dateStr = parser.getAttributeValue(null, "updatedDate");
		parser.nextTag();
		parser.require(XmlPullParser.END_TAG, ns, "file");
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FileUpdateInfo result = new FileUpdateInfo(relType, date, link);
		return result;
	}
}
